using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Diagnostics;
using FiltronASP.Helpers.HttpObjects;

namespace FiltronASP.Logger
{
    public class MyLogger
    {

        private static void LogFileAppendMessage(string message)
        {
            LoggerSectionHandler loggerSectionHandler = new LoggerSectionHandler();
            string loggerFileName = loggerSectionHandler.logFileName;
            try
            {
                loggerFileName = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(LoggerConsts.LOG_ABSOLUTE_DIR), loggerFileName);
                if (!File.Exists(loggerFileName))
                {
                    FileStream fileStream = File.Create(loggerFileName);
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }   //if not null
                }   //if file not exist
                StreamWriter streamWriter = File.AppendText(loggerFileName);
                try
                {
                    streamWriter.WriteLine("[" + DateTime.Now.ToString() + "] " + message);
                }   //try
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }   //if not null
                }   //finally
            }   //try
            catch
            {
                //do sth
            }   //catch
        }


        public static void Log(Exception ex)
        {
            try
            {
                LogFileAppendMessage("ERROR: " + ex.Message);
                LogFileAppendMessage(Environment.StackTrace);
            }   //try
            catch (Exception exception)
            {
                LogFileAppendMessage(ex.Message);
                LogFileAppendMessage(exception.Message);
            }   //catch
        }

        public static void Log(string message)
        {
            try
            {
                LogFileAppendMessage(message);
            }
            catch (Exception ex)
            {
                Log(ex);
            }

        }

    }
}
